package org.jvnc.rfb.proto.encoding;

/**
* <br><br><center><table border="1" width="80%"><hr>
* <p>
* Copyright (C) 2006-2007 by Deon Wu
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.0
* of the License, or (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">GNU Lesser General Public License</a>
* for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">
* GNU Lesser General Public License</a> along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* <hr></table></center>
* **/

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.MemoryImageSource;
import java.io.DataInput;
import java.io.IOException;

import org.jvnc.rfb.proto.Rectangle;

public class Raw extends Encoding {
	private byte[] pixels8 = null;
	private int[] pixels24 = null;
	private MemoryImageSource pixelsSource = null;
	private Image img = null;
	private ColorModel cm = null;
	private int bytesPixel = 0; 
	
	
	public Raw(Rectangle rect){
		this.rect = rect;
		int width = rect.getWidth();
		int height = rect.getHeight();
		int bytesPixel = rect.getFormat().bitsPerPixel / 8;  
		
		if(bytesPixel == 4){
			pixels24 = new int[height * width];
		    cm = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
		    pixelsSource =
		  	new MemoryImageSource(width, height, cm, pixels24, 0, width);
		}else if(bytesPixel == 1){
			pixels8 = new byte[height * width];
			cm = new DirectColorModel(8, 7, (7 << 3), (3 << 6));
		    pixelsSource =
			  	new MemoryImageSource(width, height, cm, pixels8, 0, width);
		}
		
        pixelsSource.setAnimated(true);
        img = Toolkit.getDefaultToolkit().createImage(pixelsSource);
	}

	public void fillEncoding(DataInput in) throws IOException {
		//int size = rect.getWidth() * rect.getHeight() * (rect.getFormat().bitsPerPixel / 8);
		//log.debug("Fill Raw Encoding length:" + size);
		
	    if (bytesPixel == 1) {
	    	//log.debug("Fill Raw Encoding length:" + pixels8.length);
			in.readFully(pixels8);
		} else {
			byte[] buf = new byte[pixels24.length * 4];
			//log.debug("Fill Raw Encoding length:" + buf.length);
			in.readFully(buf);
			for (int i = 0; i < pixels24.length; i++) {
				pixels24[i] = (buf[i * 4 + 2] & 0xFF) << 16
							| (buf[i * 4 + 1] & 0xFF) << 8 
							| (buf[i * 4] & 0xFF);
			}
		}
		
	}
	
	public Image deCode(Image frame){
		pixelsSource.newPixels(rect.getPositionX(), rect.getPositionY(), rect.getWidth(), rect.getHeight());
		return img;
	}

}
